(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.dynaparmController', dynaparmController);
	dynaparmController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig',
			'authService', 'ajaxServiceFactory' ];
	function dynaparmController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("dynaparmLookup");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getDynaparmTypeList : {
				url : 'do/cda/dataList/dynaparmTypeList',
				method : 'get'
			}
		});
		function getDynaparmTypeList() {
			var defer = $q.defer();
			ajaxService.getDynaparmTypeList().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			getParmTypeOptions();
			ctrl.search();
		};
		
		function getParmTypeOptions(){
			getDynaparmTypeList().then(function(response) {
				ctrl.parmTypeOptions = response;
			}, angular.noop);
		};

		ctrl.reprocess = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.reprocess(ctrl.selectedRows[0]).then(function(response) {
				var routerInfo = response.data || {}, state = routerInfo.processState;
				delete routerInfo.processState;
				$scope.go(state, routerInfo);
			}, angular.noop);
		};

		ctrl.search = function() {
			if(ctrl.dialogsModule.params){
				if(ctrl.dialogsModule.params.parmCat){
					ctrl.searchCriteria.parmCat = ctrl.dialogsModule.params.parmCat;
				}
				if(ctrl.dialogsModule.params.serviceCode){
					ctrl.searchCriteria.serviceCode = ctrl.dialogsModule.params.serviceCode;
				}
				if(ctrl.dialogsModule.params.customFlag){
					ctrl.searchCriteria.customFlag = ctrl.dialogsModule.params.customFlag;
				}
				if(ctrl.dialogsModule.params.internalFlag){
					ctrl.searchCriteria.internalFlag = ctrl.dialogsModule.params.internalFlag;
				}
			}
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);